import { _decorator, Component, instantiate, Node, Prefab } from 'cc';
import { heart } from './heart';
const { ccclass, property } = _decorator;

@ccclass('heartRoot')
export class heartRoot extends Component {
    @property(Prefab) heart:Prefab
    @property(Node) particle:Node
    chi:Node[];
    start() {
        this.create()
        console.log(this.getDownY(0))
        this.chi = this.node.children
    }
    async create()
    {
        for(let i = 0;i<1000;i++)
        {
            await new Promise(resolve => setTimeout(resolve, 5))
            const x = this.getRandom(-250,250)
            let y = 0;
            if(x < 20 && x > -20)
            {
                y = this.getRandom(this.getDownY(x),this.getTopY(x));
                y = y > -340 ? y : y+40;
            }
            else
            {
                y = this.getRandom(this.getDownY(x),this.getTopY(x));
            }
            const heart = instantiate(this.heart);
            heart.setPosition(x,y)
            this.node.addChild(heart);
            if(i === 999)
            {
                // this.action();
                this.particle.active = true;
            }
        }
    }
    async action()
    {
        for(let i = 0;i<this.chi.length;i++)
        {
            await new Promise(resolve => setTimeout(resolve, 5))
            this.chi[i].getComponent(heart).action();
        }
    }
    getTopY(x:number)
    {
        return 120 * Math.sqrt(1 - (0.008 * Math.abs(x) - 1) ** 2);
    }
    getDownY(x:number)
    {
        return 120 * (Math.acos(1 - 0.008 * Math.abs(x)) - Math.PI);
    }
    getRandom(x:number,y:number)
    {
        return x + Math.random() * (y - x);
    }
    update(deltaTime: number) {
        
    }
}


